K.App('app/common/AHeadNotice', ['core/jQuery', 'core/ajax/Ajax']).define(function(require) {

    var $ = require('core/jQuery'),
        Ajax = require('core/ajax/Ajax'),

        URL = {
            'getnotify' : '/user/ajax/alert_get.php',
            'readnotify' : '/user/ajax/alert_cancel.php'
        },

        originTitle = document.title,

        //notifyTimer = null, // 控制通知闪烁的计时器
        titleTimer = null; // 页面标题闪烁

        notifying = false;

    return {
        'container' : '#header',

        'main' : function() {
            this.initVariable();
            this.bindEvents();
            this.getNotify();
        },

        'initVariable' : function() {
            var container = this.getContainer();
            this.msgNDOM = container.find('._j_chatnum');
            this.noticeNDOM = container.find('._j_noticenum');
            this.commentNDOM = container.find('._j_cmtnum');
            this.newsNDOM = container.find('._j_newsnum');
            this.starNDOM = container.find('._j_starnum');
        },

        'bindEvents' : function() {

            // 用户mouseover到消息tab上时立即取消消息提醒闪动
            // this.getContainer().find('._j_newstab').mouseenter($.proxy(this.readNotify, this));

            // 等待消息来临事件
            K.on('notification', $.proxy(function(args) {
                var data = args.data;
                if(!K.isEmpty(data)) {
                    this.msgNotification(data, true);
                }
            }, this));


            K.on('presence', K.bind(function(category, message) {
                if (category == "alert.blink") {
                    this.stopNotify();
                }
            }, this));
        },

        'getNotify' : function() {
            Ajax.get(URL.getnotify+'?t='+(+new Date),
                $.proxy(function(data) {
                    this.web = data.web;
                    this.state = data.state;
                    this.msgNotification(data.state);
                    this.initNotification();
                }, this),
                $.proxy(function(error) {
                    K.log('Data Err:', error);
                }, this)
            );
        },

        'stopNotify': function() {
            //if (notifying) {
            //    clearInterval(titleTimer);
            //    notifying = false;
                document.title = originTitle;
            //    return true;
            //}
            //return false;
        },

        /*
        'readNotify' : function() {
            if (this.stopNotify()) {
                $.post(URL.readnotify);
            }
        },
        */

        'msgNotification' : function(data, doBlink) {
            var notifyFlag = 0;
            //clearInterval(notifyTimer);
            clearInterval(titleTimer);
            //notifying = false;
            document.title = originTitle;

            // 只在固定页面显示新消息到达提醒动画
            if(!$('body').hasClass('no_alert') && doBlink) {
                if(data.total > 0) {
                    document.title = '【新消息'+data.total+'】- '+originTitle;
                } else {
                    document.title = originTitle;
                }
                //notifying = true;
                /*
                notifyTimer = setInterval($.proxy(function() {
                    notifying = true;
                    this.newsNDOM.stop(true, true).animate({
                        'opacity' : 0.5
                    }, 100).animate({
                        'opacity' : 1
                    }, 100).animate({
                        'opacity' : 0.5
                    }, 100).animate({
                        'opacity' : 1
                    }, 100);
                }, this), 1500);
                */
                /*
                 titleTimer = setInterval($.proxy(function() {
                    if(notifyFlag % 2 === 0) {
                        document.title = '【新消息'+data.total+'】- '+originTitle;
                    } else {
                        document.title = '【　 　　】- '+originTitle;
                    }
                    notifyFlag++;
                }, this), 1500);
                */

            }

            // 私信数
            var userNum = isNaN(data.chat_user) ? 0 : parseInt(data.chat_user, 10),
                groupNum = isNaN(data.chat_group) ? 0 : parseInt(data.chat_group, 10),
                chatNum = data.chat_total;
            this.updateNews(this.msgNDOM, chatNum, (groupNum > 0 && userNum === 0) ? 'group' : 'user');

            // 评论及回复
            var replyNum = isNaN(data.reply) ? 0 : parseInt(data.reply, 10),
                cmtNum = isNaN(data.comment) ? 0 : parseInt(data.comment, 10),
                comment = data.comment_total;
            this.updateNews(this.commentNDOM, comment, (replyNum > 0 && cmtNum === 0) ? 'reply' : 'comment');

            // 通知
            var noticeNum = isNaN(data.notice) ? 0 : parseInt(data.notice, 10),
                friendNum = isNaN(data.friend) ? 0 : parseInt(data.friend, 10),
                notice = data.notice_total;
            this.updateNews(this.noticeNDOM, notice, (friendNum === 0 && noticeNum > 0) ? 'notice' : 'friend');

            // 私信数
            this.updateNews(this.starNDOM, data.notice_star);

            // 消息总数
            this.updateNews(this.newsNDOM, data.total);
        },

        'updateNews' : function(newsNDOM, newsNum, hrefType) { // 更新消息数

            // 消息数
            newsNDOM.children('em').text(newsNum);
            if(newsNum > 0) {
                newsNDOM.removeClass('dni')
            } else {
                newsNDOM.addClass('dni');
            }

            // 更新链接地址
            if(hrefType) {
                var link = newsNDOM.parent();
                if(link.attr(hrefType+'href')) {
                    link.attr('href', link.attr(hrefType+'href'));
                }
            }
        },

        'initNotification' : function() {
            var notificationDependJS = this.getNotificationDependJS(),
                notificationFrameSrc = 'http://' + this.web + '.' + K.Env.wwwHost + '/ifr/js?' + notificationDependJS;
            $('<iframe class="dni" src="' + notificationFrameSrc + '"></iframe>').appendTo('body');
        },

        'getNotificationDependJS' : function() {
            var jsHost = 'http://{_STATIC_HOST_}/',
                dependJS = [
                    'base/Haibei.js',
                    'core/jQuery.js',
                    'core/Storage.js',
                    'core/Poller.js',
                    'apps/common/ANotification.js'
                ],
                mapedJs = {},
                frameJs = [];
            K.forEach(dependJS, function(js) {
                var versionedJS = K.Resource.getResourceMap(js);
                if(!mapedJs[versionedJS]) {
                    frameJs.push('r=' + jsHost + versionedJS);
                    mapedJs[versionedJS] = true;
                }
            });
            return frameJs.join('&');
        }
    }

});
